﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.AssetManage;
using LJX.SmartPark.Domain.EnergyManage;
using LJX.SmartPark.Service.EnergyManage.Stoichio.Dto;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJX.SmartPark.Service.EnergyManage.Stoichio
{
    public class EquipmentTypeSerive : IEquipmentTypeSerive
    {
        //设备类型
        private readonly IRepository<EquipmentType> _repository;

        //能耗管理
        private readonly IRepository<LJX.SmartPark.Domain.EnergyManage.UseEnergy> _repository1;

        //设备类型
        //private readonly IRepository<EquipmentType> _repository;

        public EquipmentTypeSerive(IRepository<EquipmentType> repository, IRepository<LJX.SmartPark.Domain.EnergyManage.UseEnergy> repository1)
        {
            _repository = repository;
            _repository1 = repository1;
        }


        /// <summary>
        /// 获取设备类型全部数据
        /// </summary>
        /// <returns></returns>
        public async Task<Response<List<EquipmentType>>> GetEquipmentTypeAsync()
        {
            var students = await _repository.GetAllListAsync();

            var result = students.MapToList<EquipmentType>();

            return Response<List<EquipmentType>>.Ok(result);
        }

        /// <summary>
        /// 能耗管理删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<bool>> DeleteByIdUseEnergyAsync(LJX.SmartPark.Domain.EnergyManage.UseEnergy input)
        {
            var result = await _repository1.DeleteByIdAsync(input.MapTo<LJX.SmartPark.Domain.EnergyManage.UseEnergy>());

            return Response<bool>.Ok(result > 0);
        }

        /// <summary>
        /// 更好管理显示分页查询
        /// </summary>
        /// <param name="use"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<List<EquipmentTypeDto>>> GetUseEnergyAsync(Use use)
        {
            var students = await _repository1.GetAllListAsync();

            var students1 = await _repository.GetAllListAsync();

            var data = from a in students
                       join b in students1 on a.EquipmentTypeId equals b.Id
                       select new
                       {
                           a.Id,
                           a.UseDate,
                           a.Description,
                           a.EquipmentTypeId,
                           a.EquipmentUsage,
                           b.TypeName,
                       }.MapTo<EquipmentTypeDto>();

            var reust = data.MapToList<EquipmentTypeDto>();

            if (!string.IsNullOrWhiteSpace(use.equipmentUsage))
            {
                reust = reust.Where(x => x.EquipmentUsage.Contains(use.equipmentUsage)).ToList();
            }

            var totalCount = reust.Count();

            reust = reust.Skip(((use.pageIndex - 1) * use.pageSize)).Take(use.pageSize).ToList();

            return Response<List<EquipmentTypeDto>>.Ok(reust, totalCount);
        }

        /// <summary>
        /// 能耗管理添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<bool>> PostUseEnergy(LJX.SmartPark.Domain.EnergyManage.UseEnergy input)
        {
            var result = await _repository1.InsertAsyncById(input.MapTo<LJX.SmartPark.Domain.EnergyManage.UseEnergy>());

            return Response<bool>.Ok(result > 0);
        }

        /// <summary>
        /// 能耗管理修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PutUseEnergy(LJX.SmartPark.Domain.EnergyManage.UseEnergy input)
        {
            var result = _repository1.UpdateAsync(input.MapTo<LJX.SmartPark.Domain.EnergyManage.UseEnergy>());

            return Response<bool>.Ok(result.Result > 0);
        }
    }

    public class Equipment
    {
        public int pageIndex { get; set; }
        public int pageSize { get; set; }
    }

    public class Use
    {
        public int pageIndex { get; set; }
        public int pageSize { get; set; }
        public string equipmentUsage { get; set; }
    }
}
